package com.flame.auth.client.advice;

import com.flame.auth.core.exception.AuthExceptionMessage;
import com.flame.auth.core.exception.AuthRuntimeException;
import com.flame.core.exception.FlameRuntimeException;
import com.flame.core.protocol.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * @author wuxintong
 * @since 2023/1/1
 */
@RestControllerAdvice
public class AuthControllerAdvice {

    private static final Logger log = LoggerFactory.getLogger(AuthControllerAdvice.class);

    @ExceptionHandler(AuthRuntimeException.class)
    @ResponseBody
    public Result flameRuntimeExceptionHandler(FlameRuntimeException e, HttpServletResponse response) {
        if (AuthExceptionMessage.ACCOUNT_LOGIN_TIMEOUT.getCode().equals(e.getCode())) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
        } else {
            response.setStatus(HttpStatus.OK.value());
        }
        log.error(e.getMessage(), e);
        return Result.failure().code(e.getCode()).msg(e.getMessage());
    }
}
